package com.example.microinvestmarket.Runnable;

import java.security.SecureRandom;
import java.util.List;
import java.util.Collections;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author tiaanwailaiwu
 * 用于线程快速初始化StockName
 */

public class RandomStockNameRunnable implements Runnable {

    private static final String NAME_STRING = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final int STOCK_NAME_LENGTH = 4;
    private static final SecureRandom RANDOM = new SecureRandom();

    // 线程安全的CopyOnWriteArrayList
    private static final List<String> stockNameList = new CopyOnWriteArrayList<>();

    @Override
    public void run() {
        StringBuilder stockName = new StringBuilder(); // 包含前缀
        for (int i = 0; i < STOCK_NAME_LENGTH; i++) {
            int index = RANDOM.nextInt(NAME_STRING.length());
            stockName.append(NAME_STRING.charAt(index));
        }
        stockNameList.add(stockName.toString());
    }

    // 返回不可修改的列表视图，防止外部修改
    public static List<String> getStockNameList() {
        return Collections.unmodifiableList(stockNameList);
    }

    // 新增：清空股票名称列表的方法
    public static void clearStockNameList() {
        stockNameList.clear();
    }

}
